package de.dopichaj.labrador.search.own;


import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import de.dopichaj.labrador.index.Tokenizer;
import de.dopichaj.labrador.index.backend.ReadOnlyDictionary;
import de.dopichaj.labrador.index.backend.Term;
import de.dopichaj.labrador.search.Query;
import de.dopichaj.labrador.search.QueryParseException;

import de.dopichaj.labrador.search.NEXIParser;
import antlr.RecognitionException;
import antlr.TokenStreamException;

public final class OwnQuery implements Query {

    private static final Logger log = Logger.getLogger(OwnQuery.class);
    
    private final Set<Term> queryTerms = new HashSet<Term>();
    private final ReadOnlyDictionary dictionary;
    private final Tokenizer tokenizer;
    
    public OwnQuery(final String nexi, final Tokenizer tokenizer, final ReadOnlyDictionary dictionary) throws QueryParseException {
        
        this.dictionary = dictionary;
        this.tokenizer = tokenizer;
        de.dopichaj.labrador.nexi.NEXIParser parser = new de.dopichaj.labrador.nexi.NEXIParser();
        NEXIParser converter = new NEXIParser(this);
        try {
            converter.query(parser.parse(nexi));
        } catch (RecognitionException e) {
            throw new QueryParseException("Error parsing query: " + e.getMessage());
        } catch (TokenStreamException e) {
            throw new QueryParseException("Error parsing query: " + e.getMessage());
        }
    }
    
    public void addPhraseQuery(String category, List<String> phrase, float boost) {
        for (final String term : phrase) {
            addTermQuery(category, term, boost);
        }
    }

    public void addTermQuery(String category, String term, float boost) {
        
        for (final String subTerm : parseTerm(term)) {
            Term realTerm = null;
            realTerm = dictionary.getTerm(subTerm);
            if (realTerm != null) {
                queryTerms.add(realTerm);
            } else {
                log.debug("Query term " + subTerm + " is not in the dictionary!");
            }
        }
    }

    private List<String> parseTerm(String word) {
        log.debug("Parsing " + word);
        return tokenizer.tokenize(word);
    }

    public void setTargetPath(String path) {
        // currently not supported
    }
    
    public final Collection<Term> getQueryTerms() {
        return Collections.unmodifiableCollection(queryTerms);
    }
}
/*
Copyright (c) 2005-2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/